﻿using AutoMapper;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Read.Api.Handler.Tranceabilty
{
    public class EchoTraceCommandHandler : IRequestHandler<GetTranceShuoYuanByIdCommand, ApiResult>
    {
        private readonly ItraceabilitableRepository _traceabilitableRepository;

        private readonly IMapper _mapper;

        private readonly ILogger<EchoTraceCommandHandler> _logger;

        private readonly IGoodsRepository _goodsRepository;

        public EchoTraceCommandHandler(ItraceabilitableRepository traceabilitableRepository, IMapper mapper, ILogger<EchoTraceCommandHandler> logger, IGoodsRepository goodsRepository)
        {
            _traceabilitableRepository = traceabilitableRepository;
            _mapper = mapper;
            _logger = logger;
            _goodsRepository = goodsRepository;
        }
        public async Task<ApiResult> Handle(GetTranceShuoYuanByIdCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var trace = await _traceabilitableRepository.GetAsync(x=>x.TrId==request.TrId);
                if (trace==null)
                {
                    return   ApiResult.Fail( "回显失败");
                }
                return  ApiResult.Ok(trace);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error when get trace by id");
                throw;
            }
        }
    }
}
public class GetTranceShuoYuanByIdCommand:IRequest<ApiResult>
{
    public int TrId { get; set; }
}